﻿using BanquetGift.Command;
using BanquetGift.DataBases;
using BanquetGift.Dto;
using BanquetGift.Models;
using System.Collections.Generic;
using System.Linq;

namespace BanquetGift.Services
{
    public interface IDataDictionaryRepository
    {
        DataDictionary CreateDataDictionary(DataDictionaryDto dataDictionaryDto);
        DataDictionary EditDataDictionary(DataDictionaryDto dataDictionaryDto,int dataDictionaryId);
        bool DeleteDataDictionaryById(int dataDictionaryId);
        IEnumerable<DataDictionary> GetDataDictionaryByParentId(int ParentId = 0);
        DataDictionaryExtention GetDataDictionaryById(int dataDictionaryId);
        bool IsExistDataDictionary(int dataDictionaryId);
    }
    public class DataDictionaryRepository : IDataDictionaryRepository
    {
        private readonly BGDbContext _bGDbContext;

        public DataDictionaryRepository(BGDbContext bGDbContext)
        {
            _bGDbContext = bGDbContext;
        }

        public DataDictionary CreateDataDictionary(DataDictionaryDto dataDictionaryDto)
        {
            DataDictionary dataDictionary = AutoMapperHelp.MappingTo<DataDictionaryDto, DataDictionary>(dataDictionaryDto);
            _bGDbContext.DataDictionaries.Add(dataDictionary);
            _bGDbContext.SaveChanges();
            return dataDictionary;
        }

        public bool DeleteDataDictionaryById(int dataDictionaryId)
        {
            DataDictionary dataDictionary= _bGDbContext.DataDictionaries.FirstOrDefault(dD => dD.Id == dataDictionaryId);
            _bGDbContext.DataDictionaries.Remove(dataDictionary);
            return _bGDbContext.SaveChanges() == 1;
        }

        public DataDictionary EditDataDictionary(DataDictionaryDto dataDictionaryDto, int dataDictionaryId)
        {
            DataDictionary dataDictionary = _bGDbContext.DataDictionaries.FirstOrDefault(dD => dD.Id == dataDictionaryId);
            dataDictionary.ParentId = dataDictionaryDto.ParentId;
            dataDictionary.DtDicName = dataDictionaryDto.DtDicName;
            dataDictionary.Description = dataDictionaryDto.Description;
            dataDictionary.SortIndex = dataDictionaryDto.SortIndex;
            _bGDbContext.DataDictionaries.Update(dataDictionary);
            _bGDbContext.SaveChanges();
            return dataDictionary;
        }

        public DataDictionaryExtention GetDataDictionaryById(int dataDictionaryId)
        {
            DataDictionary dataDictionary = _bGDbContext.DataDictionaries.FirstOrDefault(dD => dD.Id == dataDictionaryId);
            DataDictionaryExtention dataDictionaryExtention =
                AutoMapperHelp.MappingTo<DataDictionary, DataDictionaryExtention>(dataDictionary);
            dataDictionaryExtention.Child = _bGDbContext.DataDictionaries.Where(dD => dD.ParentId == dataDictionaryId).ToList();
            return dataDictionaryExtention;
        }

        public IEnumerable<DataDictionary> GetDataDictionaryByParentId(int ParentId = 0)
        {
            IEnumerable<DataDictionary> dataDictionaries = _bGDbContext.DataDictionaries.Where(dD => dD.ParentId == ParentId);
            return dataDictionaries;
        }

        public bool IsExistDataDictionary(int dataDictionaryId)
        {
            DataDictionary dataDictionary = _bGDbContext.DataDictionaries.FirstOrDefault(dD => dD.Id == dataDictionaryId);
            return dataDictionary != null;
        }
    }
}
